package com.assignment.main;

import java.awt.image.BufferedImage;
import java.io.File;

import javax.swing.ImageIcon;

import com.assignment.database.DBOperations;
import com.assignment.gui.EditItemScreen;
import com.assignment.gui.SearchScreen;
import com.assignment.gui.ViewItemScreen;

/**
 * 
 * 
 * Class to create objects from the underlying database table Item, which
 * can then be manipulated within java and inserted back into the database.
 * 
 * @author John Wright and Luke Williams
 * 
 * @version 1.0
 */
public class Item implements Selectable {

	public static final int FIREPLACE = 0;
	public static final int STOVE = 1;

	public String typeNames[] = { "Fireplace", "Stove" };

	private String id, name, description, supplierId;
	private int type, stock, style;
	private double buyPrice, sellPrice;
	private ImageIcon image;
	private File file;

	/**
	 * Constructor method which takes a String array, from which initial values
	 * are set.
	 * 
	 * @param data
	 *            Array of Item attributes
	 */
	public Item(String data[]) {

		System.out.println("Type: " + data[3]);
		if (data[0] != null)
			id = data[0].replaceAll("\n", "").trim();
		name = data[1];
		description = data[2];
		type = Integer.valueOf(data[3].replaceAll("\n", "").trim());
		stock = Integer.valueOf(data[4]);
		buyPrice = Double.valueOf(data[5]);
		sellPrice = Double.valueOf(data[6]);
		style = Integer.valueOf(data[7]);

		supplierId = data[9].replaceAll("\n", "").trim();
	}

	/**
	 * Method to get the id of the item.
	 * 
	 * @return id the Item id
	 */
	public String getId() {
		return id;
	}

	/**
	 * Method to set the id of an item.
	 * 
	 * @param id
	 *            The Item id
	 * 
	 * 
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * Method to get the name of an item
	 * 
	 * @return name The item name
	 * 
	 *         .
	 */
	public String getName() {
		return name;
	}

	/**
	 * Method to set the name of an item.
	 * 
	 * @param name
	 *            The item name
	 * 
	 * 
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Method to get the Items description
	 * 
	 * @return description the item description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * 
	 * Method to set the description of an item.
	 * 
	 * @param description
	 *            The item description
	 * 
	 * 
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * 
	 * Method to get the type of an item, corresponds to the final int values
	 * FIREPLACE and STOVE belonging to this class
	 * 
	 * @return type The item type
	 * 
	 * 
	 */
	public int getType() {
		return type;
	}

	/**
	 * 
	 * Method to set the type of an item, corresponds to the final int values
	 * FIREPLACE and STOVE belonging to this class
	 * 
	 * @param type
	 *            The item type
	 * 
	 * 
	 */
	public void setType(int type) {
		this.type = type;
	}

	/**
	 * 
	 * Method to get the stock/quantity of an item.
	 * 
	 * @return stock The stock quantity
	 * 
	 * 
	 */
	public int getStock() {
		return stock;
	}

	/**
	 * Method to set the stock/quantity of an item.
	 * 
	 * @param stock
	 *            The stock quantity
	 * 
	 * 
	 */
	public void setStock(int stock) {
		this.stock = stock;
	}

	/**
	 * 
	 * Method to get the style of an item
	 * 
	 * @return style The item style
	 * 
	 * 
	 */
	public int getStyle() {
		return style;
	}

	/**
	 * 
	 * Method to set the style of an item.
	 * 
	 * @param style
	 *            The item style
	 * 
	 * 
	 */
	public void setStyle(int style) {
		this.style = style;
	}

	/**
	 * 
	 * Method to get the supplierId for an item object, relating to the id
	 * attribute of the supplier class
	 * 
	 * @return supplierId The supplier id for the item
	 * 
	 * 
	 */
	public String getSupplierId() {
		return supplierId;
	}

	/**
	 * 
	 * Method to set the supplierId for an item object, relating to the id
	 * attribute of the supplier class
	 * 
	 * @param supplierId
	 *            The supplier id for the item
	 * 
	 * 
	 */
	public void setSupplierId(String supplierId) {
		this.supplierId = supplierId;
	}

	/**
	 * 
	 * Method to get the buyPrice of an item object.
	 * 
	 * @return buyPrice The buy price of the item
	 * 
	 * 
	 */
	public double getBuyPrice() {
		return buyPrice;
	}

	/**
	 * 
	 * Method to set the buyPrice for an item object.
	 * 
	 * @param buyPrice
	 *            The buy price of the item
	 * 
	 * 
	 */
	public void setBuyPrice(double buyPrice) {
		this.buyPrice = buyPrice;
	}

	/**
	 * 
	 * Method to get the sellPrice of an item object.
	 * 
	 * @return sellPrice The sell price of the item
	 * 
	 * 
	 */
	public double getSellPrice() {
		return sellPrice;
	}

	/**
	 * 
	 * Method to set the sellPrice for an item object.
	 * 
	 * @param sellPrice
	 *            The sell price of the item
	 * 
	 * 
	 */
	public void setSellPrice(double sellPrice) {
		this.sellPrice = sellPrice;
	}

	/**
	 * 
	 * Method to get the image of an item object.
	 * 
	 * @return image The item image
	 * 
	 * 
	 */
	public ImageIcon getImage() {
		return image;
	}

	/**
	 * 
	 * Method to set the image of an item object.
	 * 
	 * @param image
	 *            The item image
	 * 
	 * 
	 */
	public void setImage(ImageIcon image) {
		this.image = image;
	}

	/**
	 * 
	 * Method to get file of the image for an item object.
	 * 
	 * @return file The raw file version of the item image
	 * 
	 * 
	 */
	public File getFile() {
		return file;
	}

	/**
	 * 
	 * Method to set the file of the image for an item object.
	 * 
	 * @param file
	 *            The raw file version of the item image
	 * 
	 */
	public void setFile(File file) {
		this.file = file;
	}

	/**
	 * Override of the toString() method in the object class, to show the name
	 * and type of the item when it is added to a JList object in the
	 * SearchScreen
	 * 
	 * @return A String representation of the item
	 */
	@Override
	public String toString() {

		return String.format("%s: %s", typeNames[type], name);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.assignment.main.Selectable#onSelectToView(com.assignment.gui.SearchScreen
	 * )
	 */
	@Override
	public void onSelectToView(SearchScreen screen) {
		new ViewItemScreen(this, screen).setVisible(true);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.assignment.main.Selectable#onSelectToEdit(com.assignment.gui.SearchScreen
	 * )
	 */
	@Override
	public void onSelectToEdit(SearchScreen screen) {
		new EditItemScreen(this, screen).setVisible(true);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.assignment.main.Selectable#onSelectToDelete()
	 */
	@Override
	public void onSelectToDelete() {

		DBOperations.deleteItem(this);

	}

}
